  # These are the methods for loading for the no mapping of tables but simply for replicating tables.
  def load_all_tables (&b)
    all_tables_hash = b.call
    all_tables_hash.each{|source,destination|
      all_tables = source.tables
      table_hash={:table_names=>all_tables}
      Loading.db_to_db_load(source,destination,table_hash)
    }
  end


  #This method reads the config file for the loading process.
  def read_load_file
    extract_options = String.new
    FasterCSV.foreach("load_config.csv") do |row|
      for field_counter in 0..row.length
        if row[field_counter] !="csv_files"
          if field_counter == 0  
            extract_options = extract_options+"#{row[field_counter]}"
          else
            extract_options = extract_options+",#{row[field_counter]}"	
          end	
        end
      end	
    end	
    return extract_options	 
  end
 
 
 
  #This method returns the CSV files which are to be loaded into the table.
  def get_csv_file_list
    csv_file_list = String.new 
    FasterCSV.foreach("load_config.csv") do |row|
      for field_counter in 0..row.length
        if row[field_counter] == "csv_files"
          for csv_file_counter in (field_counter+1)..row.length
            if csv_file_counter == row.length
              csv_file_list = csv_file_list+"#{row[csv_file_counter]}"
            else
              csv_file_list = csv_file_list+"#{row[csv_file_counter]},"
            end	
          end
          return csv_file_list
        end	
      end
    end	
  end


 
  #This method checks the number of columns in a CSV file. This will be used while creating the dummy_headers.
  def get_number_of_columns_for_csv_file(filename)
    row_counter = 0
    @no_of_fields = []
    FasterCSV.foreach(filename) do |row|
      @no_of_fields[row_counter] = row.size
      row_counter+=1
    end
    return @no_of_fields.sort[@no_of_fields.size-1]	
  end



  #This is main method of data loading. This method decided if the loading is to be done for a single CSV file or multiple.
  def entry_method_for_process_to_start(filename)
    if filename.split(",").size > 1
      filename_arr = filename.split(",")
      for file_counter in 0..filename_arr.size-1
        load_csv_to_table(filename_arr[file_counter])
      end
    else
      load_csv_to_table(filename)
    end	
  end	



  #This method sets all the empty values as nil so as to simplify the loading process.
  def set_blank_values_as_nil(filename)
    FasterCSV.foreach(filename) do |row|
      for field_counter in 0..row.length
        row[field_counter] = 'nil' if row[field_counter] == ' '
      end
    end 
  end	 


 
  #This method has been now updated so that it can allow the creation of the tables without the preexisting the header files.
  def get_table_header_for_csv(full_path,filename,extension)
    filename = filename.split(".")[0]
    header_filename = filename+"_header.csv"	
    header_fields = [] 
    header_filename = full_path+header_filename if full_path != ""
    filename = full_path+filename if full_path != ""
    if File.exist?(header_filename)
      FasterCSV.foreach(header_filename) do |row|
        for field_counter in 0..row.length
          if row[field_counter] != nil
            header_fields << row[field_counter]
          end	
        end
      end
      return header_fields
    else
      filename = filename+".csv"
      if File.exists? filename
        FasterCSV.foreach(filename) do |row|
          for field_counter in (0..row.length-1)
            row[field_counter] = row[field_counter].gsub(" ","_") if !row[field_counter].nil?
            header_fields << row[field_counter]
          end
          $headers_set_from_csv  = true
          break
        end	
        set_blank_values_as_nil(filename)
        total_columns = get_number_of_columns_for_csv_file(filename)
      else
        puts "The datasource CSV file is missing from the current working directory."
        return false
      end		
    end	
    return header_fields        
  end	 



  #This method checks if there is already a predefined structure file for the table.
  def structure_file_exists(table_name)
    if File.exist?("#{table_name}_structure.csv")
      return true
    else
      return false
    end	
  end


  #This is the actual load method which will be load the data to the table.
  def load_csv_to_table(filename)
    new_conn = create_new_connection
    @start_time_loading = Time.now
    execute_loading("",filename,"",new_conn)
  end


  # This method along with the method in Normalize:Splitter will be taken common in app_wide_methods.
  def generate_missing_table_name(table_name,connection)
    all_tables = connection.tables
    for table in all_tables
      if table.to_s == table_name
        table_name = table_name+"new"
      end  
    end
    return table_name  
  end



  # This method will tell if the tablename exists under the database connected with new_conn
  def table_already_exists(new_conn,tablename)
    if new_conn.tables.size == 0  
      return false
    else
      new_conn.tables.each{|table|
        if table.to_s == tablename
          return true
        else  
          return false
        end	
      }
    end        
  end	 


  # This method will execute the partial loading i.e. loading of selected columns.
  def execute_partial_column_loading(column_list,table_name,source_connection,destination_connection,from_final_load)
    actual_table_name = ""
    header_file = generate_header_file(table_name,source_connection,column_list)
    extract_column(column_list,table_name,source_connection,"some")
    row_array = []
    FasterCSV.foreach("#{table_name}_selected_some_columns.csv")do|row|
      row_array << row
    end  
    row_array = row_array.transpose
    new_file = File.new("#{table_name}.csv","w")
    row_array.each{|data|
      new_file.write("\"")
      new_file.write(data.join("\",\""))
      new_file.write("\"")
      new_file.write("\n")
    }
    new_file.close
    table_name = execute_loading("","#{table_name}.csv",".csv",source_connection)
    if from_final_load
      load_data(
      { 
        :destination=>destination_connection,
        :source=>source_connection,
        ["#{table_name}"]=>"#{from_final_load}"
      })  
    end    
  end  


  # This method is executing the loading of the entire files.
  def execute_loading(full_path,filename,extension,new_conn)
    row_counter = 0 
    filename = filename.gsub(/,/,'')
    original_tablename = filename.gsub("#{extension}",'')
    tablename = original_tablename.gsub(/\,/,'')
    tablename = generate_missing_table_name(tablename,new_conn)
    tablename = tablename.split("/")[1] if !tablename.scan("/").empty?
    if !table_already_exists(new_conn,tablename)
      tables_header = get_table_header_for_csv(full_path,filename,extension)
      if tables_header
        for column in 0..tables_header.length-1
          if tables_header[column] != nil
            if structure_file_exists(tablename)
              column_details = get_the_col_specs(tables_header[column],tablename)
            else
              column_details = "text"#"varchar(255)"
            end		
            if column == 0 
              new_conn.execute("CREATE TABLE #{tablename}(#{tables_header[column]} #{column_details})")
            else
              new_conn.execute("ALTER TABLE #{tablename} ADD `#{tables_header[column]}` #{column_details}")
            end
          end	
        end          
        filename = original_tablename+".csv"
        filename = full_path+filename if full_path != ""
        FasterCSV.foreach(filename) do |row|
          @connected_row=String.new
          if row.size!=0
            for field in row
              field = field.gsub("\""," ") if !field.nil?
              @connected_row << "\"#{field}\","
            end
            @connected_row[@connected_row.length-1]=''
            if $headers_set_from_csv && row_counter == 0
              puts "Skipping the header fields record."  
              $headers_set_from_csv = false
            else  
              new_conn.execute("Insert into #{tablename} values(#{@connected_row})")
            end    
          end
          row_counter = row_counter + 1
        end	
      end	 
    else
      puts "The table is already exists in the database. Please rename the CSV file"	
    end		
    return tablename  
  end